package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollectionUtil;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FormInstanceDraft;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FormInstanceDraftRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowFormInstanceDraft;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowFormInstanceDraftExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowFormInstanceDraftDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lilh
 * @date 2021/5/28 10:30
 */
@Repository
public class FormInstanceDraftRepositoryImpl implements FormInstanceDraftRepository {

    @Autowired
    private FlowFormInstanceDraftDAO formInstanceDraftDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addFormInstanceDraft(FormInstanceDraft formInstanceDraft) {
        //1.更新最新的一条记录的latest_update为0
        updateLatestDraftToHistory(formInstanceDraft.getTaskId());
        //2.新增一条表单填报记录
        return doInsertFormInstanceDraft(formInstanceDraft);
    }

    private boolean doInsertFormInstanceDraft(FormInstanceDraft formInstanceDraft) {
        FlowFormInstanceDraft record = new FlowFormInstanceDraft();
        BeanCopierUtils.copyProperties(formInstanceDraft, record);
        //设置为最新记录
        record.setLatestUpdate((byte) 1);
        return formInstanceDraftDAO.
                insertSelective(record) > 0;
    }

    private void updateLatestDraftToHistory(String taskId) {
        FlowFormInstanceDraft entity = new FlowFormInstanceDraft();
        entity.setLatestUpdate((byte) 0);
        FlowFormInstanceDraftExample queryCondition = new FlowFormInstanceDraftExample();
        queryCondition.createCriteria().andLatestUpdateEqualTo((byte) 1).andTaskIdEqualTo(taskId);
        formInstanceDraftDAO.updateByExampleSelective(entity, queryCondition);
    }

    @Override
    public FormInstanceDraft getLatestFormInstanceDraft(String taskId) {
        FlowFormInstanceDraftExample queryCondition = new FlowFormInstanceDraftExample();
        queryCondition.createCriteria().andTaskIdEqualTo(taskId).andLatestUpdateEqualTo((byte) 1);
        return getFormInstanceDraft(queryCondition);
    }

    @Override
    public FormInstanceDraft getLatestFormInstanceDraft(String processInstanceId, String nodeCode) {
        FlowFormInstanceDraftExample queryCondition = new FlowFormInstanceDraftExample();
        queryCondition.createCriteria()
                .andProcessInstanceIdEqualTo(processInstanceId)
                .andTaskNodeCodeEqualTo(nodeCode)
                .andLatestUpdateEqualTo((byte) 1);
        queryCondition.setOrderByClause(" update_time desc");
        return getFormInstanceDraft(queryCondition);
    }

    private FormInstanceDraft getFormInstanceDraft(FlowFormInstanceDraftExample queryCondition) {
        List<FlowFormInstanceDraft> flowFormInstanceDrafts = formInstanceDraftDAO.selectByExampleWithBLOBs(queryCondition);
        FlowFormInstanceDraft latestFormInstanceDraft = null;
        if (CollectionUtil.isNotEmpty(flowFormInstanceDrafts) && Objects.nonNull(latestFormInstanceDraft = flowFormInstanceDrafts.get(0))) {
            FormInstanceDraft result = new FormInstanceDraft();
            BeanCopierUtils.copyProperties(latestFormInstanceDraft, result);
            return result;
        }
        return null;
    }
}
